<?php
/**
 * Created by PhpStorm.
 * User: dev
 * Date: 3/11/14
 * Time: 11:27 AM
 */

class Site_Search_Controller extends Base_Controller {

	public $restful = true;

	public function __construct()
	{
		parent::__construct();
		//--Your Code --//
		$this->theme->set_theme("default") ;
		$this->theme->set_layout('column3');
	}

	public function post_services(){

		$term = (int) Input::get('term');
		
			
		$location = (int) Input::get('location');
		
	//	$loc =  Cookie::get('location_cookie');
		$loc = Input::get('loc');
		$locname = Locations::where('slug','=',$loc)->first();
		if($locname){
		$loc = $locname->id ;
		}else{
			$loc = '';
		}
		
		//dd($location);
		//$location	= Settings::get('en','default_location');
		
		

		$ct = Categories::find($term)->slug;

		if(($location) ){
			//dd("INPUT");
			$cl = Locations::find($location)->slug;
			Location::set($location);
			return Redirect::to (Str::slug($cl).'/'.Str::slug($ct));
		}if($loc){
			$sessloc = $locname->slug;
			Location::set($loc);
			return Redirect::to (Str::slug($sessloc).'/'.Str::slug($ct));
		}if(!$location && !$loc){
			$defaultloc = Settings::get('en','default_location');
			Location::set(Settings::get('en','default_location'));
			return Redirect::to (Str::slug($defaultloc).'/'.Str::slug($ct));
		}
		
		
		
		
	}
	
	

	public function post_business(){

		$data['mode'] = "LISTING";
		$term = (int) Input::get('selectbox-business');
		$location = (int) Input::get('selectbox-location2');

		$loc = Input::get('loc');
		$locname = Locations::where('slug','=',$loc)->first();
		if($locname){
		$loc = $locname->id ;
		}else{
			$loc = '';
		}
		
		//   dd($location);
		$offset = 0 ; $limit = 10;
		$title = Post::find($term)->title;

		if($location){
			
		$sql = "SELECT P.* FROM post AS P
		LEFT JOIN terms AS T ON P.id = T.post_id WHERE P.title like '%$title%' and T.value= $location and T.term = 'location' limit $offset,$limit
		";
		
		}if($loc){
			
		
			$sql = "SELECT P.* FROM post AS P
			LEFT JOIN terms AS T ON P.id = T.post_id WHERE P.title like '%$title%' and T.value='$loc' and T.term = 'location' limit $offset,$limit
		
			";
			
		}if(!$location && !$loc){
			
			$sql = "Select * from post where title like '%$title%' limit $offset,$limit";
		}
		
		$result = DB::query($sql);
		
		$post = Post::find($term)->slug;
		
		
		if(($location) ){
			$inputloc = Locations::find($location)->slug;
			Location::set($location);
			return Redirect::to ('business'.'/'.$inputloc.'/'.$post);
		}if($loc){
			$sesloca = $locname->slug;
			Location::set($loc);
			return Redirect::to ('business'.'/'.$sesloca.'/'.$post);
		}if(!$location && !$loc){
			$notloation = Settings::get('en','default_location');
			Location::set(Settings::get('en','default_location'));
			return Redirect::to ('business'.'/'.$notloation.'/'.$post);
		}

		

	}

	public function get_business($cl,$post){
		
		
		
		$data['mode'] = "LISTING";
		
		
		$loc = $this->SlugID('locations',$cl);
		
		$post = Post::where('slug','=',$post)->first();
		if($loc){
		$loc = $loc;
		
		}
		$title = $post->title;
		
		$data['title'] = $title;
		
		$offset = 0 ; $limit = 10;
		if($loc){
		$sql = "SELECT P.* FROM post AS P
		LEFT JOIN terms AS T ON P.id = T.post_id WHERE P.title like '%$title%' and T.value= $loc and T.term = 'location' limit $offset,$limit
		";
		}if(!$loc){
			
			$sql = "Select * from post where title like '%$title' limit $offset,$limit";
			
		}
		$result = DB::query($sql);
		
		$data['listings'] = $result;
		
		
		
		/*-- BREADCRUMB */
		 
		if(($loc) ){
			$data['location'] = Locations::find($loc)->name;
			$data['loc'] = Locations::find($loc)->prefix;
			Location::set($loc);
			
			/*-- META DATA */
			$this->theme->title($data['title'].' in '.$data['location'].' - '.Settings::get('en','site_title'));
		}else{
			$data['location'] = Settings::get('en','default_location');
			
			$data['loc'] = '';
			Location::set(Settings::get('en','default_location'));
			
			/*-- META DATA */
			$this->theme->title($data['title'].' in '.$data['location'].' - '.Settings::get('en','site_title'));
		}
		
		Breadcrumb::addbreadcrumb('Home', URL::base());
		Breadcrumb::addbreadcrumb($data['location'], URL::base());
		Breadcrumb::addbreadcrumb($title, URL::base());
		Breadcrumb::setSeparator('&raquo;');
		$data['breadcrumb'] = Breadcrumb::generate();
		
		
		return $this->theme->render('site::listing',$data);
	}

}